package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.Empresa;

public class TransportadoraDAO {
	


public boolean insert(Empresa t){
		
		String sql = "insert into transportadora values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
		
		
		try{
			
			  	
			   PreparedStatement prep = ConnectionFactory.getInstance().prepareStatement(sql);
			   prep.setString (1, t.getStrRazaoSocial());
			   prep.setString (2, t.getCNPJ());
			   prep.setString (3, t.getLogradouro());
			   prep.setString (4, t.getComplemento());
			   prep.setInt(5, t.getNum());
			   prep.setString (6, t.getBairro());
			   prep.setString (7, t.getCEP());
			   prep.setString (8, t.getCidade());
			   prep.setString (9, t.getEstado());
			   
			   prep.execute();
			   
			   ConnectionFactory.getInstance().close();
			   
			   return true;
			   
	
		}catch(SQLException e){
			
			e.getMessage();
			return false;
			
		}catch(Exception ex){
			
			ex.getMessage();
			return false;
			
		}
		

	
	}
	/*
	public boolean update(Empresa t){
		
		
		String sql = "update transportadora set razao_social = ?, cnpj = ?, logradouro = ?, complemento = ?, numero = ?, bairro = ?, cep = ?, cidade = ?, estado = ?";
		
		
		try{
			
			  	
			   PreparedStatement prep = ConnectionFactory.getInstance().prepareStatement(sql);
			   prep.setString (1, t.getStrRazaoSocial());
			   prep.setString (2, t.getCNPJ());
			   prep.setString (3, t.getLogradouro());
			   prep.setString (4, t.getComplemento());
			   prep.setInt(5, t.getNum());
			   prep.setString (6, t.getBairro());
			   prep.setString (7, t.getCEP());
			   prep.setString (8, t.getCidade());
			   prep.setString (9, t.getEstado());
			   
			   prep.execute();
			   
			   ConnectionFactory.getInstance().close();
			   
			   return true;
			   
	
		}catch(SQLException e){
			
			e.getMessage();
			return false;
			
		}catch(Exception ex){
			
			ex.getMessage();
			return false;
			
		}	
	
		
	}
	
	*/

/*
	public boolean delete(Empresa t){
		
		
		String sql = "delete from transportadora where cnpj = ?";
		
		
		try{
			
			  	
			   PreparedStatement prep = ConnectionFactory.getInstance().prepareStatement(sql);
			   prep.setString (1, t.getCNPJ());
			 
			   prep.execute();
			   
			   ConnectionFactory.getInstance().close();
			   
			   return true;
			   
	
		}catch(SQLException e){
			
			e.getMessage();
			return false;
			
		}catch(Exception ex){
			
			ex.getMessage();
			return false;
			
		}	

	}
*/
	public static ResultSet selectALL(Empresa t){
		
		
		String sql = "select * from transportadora where cnpj = ?";	
		
		try{
			
		  	
			   PreparedStatement prep = ConnectionFactory.getInstance().prepareStatement(sql);
			   prep.setString (1, t.getCNPJ());
			 
			   ResultSet rs = prep.executeQuery();
	
			   ConnectionFactory.getInstance().close();
			   
			   if(rs.next()) 
				   return rs;
			   else 
				   return null;
	
	
		}catch(SQLException e){
			
			e.getMessage();
			return null;
			
		}catch(Exception ex){
			
			ex.getMessage();
			return null;
			
		}	

	}
	
	public static ResultSet selectALL(String cnpj) throws SQLException, Exception{
		
		String sql = "select * from transportadora where cnpj = ?";	
		
		try{
			
		  	
			   PreparedStatement prep = ConnectionFactory.getInstance().prepareStatement(sql);
			   prep.setString (1, cnpj);
			 
			   ResultSet rs = prep.executeQuery();

			   ConnectionFactory.getInstance().close();
			   
			  return rs;
		
	
		}catch(SQLException e){
			
			e.getMessage();
			return null;
			
		}catch(Exception ex){
			
			ex.getMessage();
			return null;
			
		}
			

	}
	
	
	
	public static ResultSet selectALL(){
		
		
		String sql = "select * from transportadora";	
		
		try{
			
		  	
			   PreparedStatement prep = ConnectionFactory.getInstance().prepareStatement(sql);
			 
			   ResultSet rs = prep.executeQuery();
			   
			   ConnectionFactory.getInstance().close();
			   
			   return rs;
			   
	
		}catch(SQLException e){
			
			e.getMessage();
			return null;
			
		}catch(Exception ex){
			
			ex.getMessage();
			return null;
			
		}	

	}

}
